package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.dsp.bank.directbank.bankInterface.IQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.ReqQryQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfoDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

/**
 * 获取额度信息
 *
 * @author marlon
 * @date 2019/1/9
 */
@Component("CMBC01QryQuotaInfo")
public class CMBC01QryQuotaInfo extends CMBC01Base implements IQuotaInfo {
    @Override
    public RetAtsQuotaInfo queryQuotaInfo(ReqQryQuotaInfo qryQuotaInfo) throws Exception {
        RetAtsQuotaInfo retAts = new RetAtsQuotaInfo(qryQuotaInfo.reqHeader);
        retAts.writeStart();
        try {
            CMBC01Header bankHeader = new CMBC01Header();
            bankHeader.trnCode = "XR205003";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, bankHeader);
            XElement tmpXElement = new XElement("xDataBody", "");
            tmpXElement.addChild(new XElement("xskhdm", qryQuotaInfo.DealerCode));
            tmpXElement.addChild(new XElement("xtbaol", ""));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();//写入报文完成

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
                    String tmpRetMessage = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
                    if ("0".equals(tmpRetCode)) {
                        String xtcsbz = tmpBkReader.getFirstText("/CMBC/xDataBody/XTCSBZ");
                        if ("01".equals(xtcsbz)){
                            retAts.writeRespCode("0");
                            retAts.writeRespInfo(tmpRetMessage);
                            while (tmpBkReader.read("/CMBC/xDataBody/Detail")) {
                                RetAtsQuotaInfoDetail tmpRetDetail = new RetAtsQuotaInfoDetail();
                                tmpRetDetail.DealerCode = qryQuotaInfo.DealerCode;
                                tmpRetDetail.LimitBeginDate = tmpBkReader.readedXElement.getSingleText("EDQSRQ");
                                tmpRetDetail.LimitEndDate = tmpBkReader.readedXElement.getSingleText("EDJSRQ");
                                tmpRetDetail.Limit = tmpBkReader.readedXElement.getSingleText("XSZHZE");
                                tmpRetDetail.UsedLimit = tmpBkReader.readedXElement.getSingleText("");
                                tmpRetDetail.UnuseLimit = tmpBkReader.readedXElement.getSingleText("XSKYZE");
                                tmpRetDetail.Reserve1 = "";
                                tmpRetDetail.Reserve2 = "";
                                tmpRetDetail.Reserve3 = "";
                                tmpRetDetail.Reserve4 = "";
                                tmpRetDetail.Reserve5 = "";
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        }else {
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(tmpBkReader.getFirstText("/CMBC/responseHeader/xtsbyy"));
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpRetCode + tmpRetMessage);
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpSbMsg.toString());
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }

        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
        }
        retAts.writeEnd();
        return retAts;

    }
}


